package com.ruoyi.xdata.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.xdata.task.KettleTask;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xdata.domain.XdataKettle;
import com.ruoyi.xdata.service.IXdataKettleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 同步任务Controller
 * 
 * @author yepanpan
 * @date 2025-02-14
 */
@RestController
@RequestMapping("/xdata/kettle")
public class XdataKettleController extends BaseController
{
    @Autowired
    private IXdataKettleService xdataKettleService;

    /**
     * 查询同步任务列表
     */
    @GetMapping("/select")
    public TableDataInfo select(XdataKettle xdataKettle)
    {
        List<XdataKettle> list = xdataKettleService.selectXdataKettleList(xdataKettle);
        return getDataTable(list);
    }

    /**
     * 查询同步任务列表
     */
    @PreAuthorize("@ss.hasPermi('xdata:kettle:list')")
    @GetMapping("/list")
    public TableDataInfo list(XdataKettle xdataKettle)
    {
        startPage();
        List<XdataKettle> list = xdataKettleService.selectXdataKettleList(xdataKettle);
        return getDataTable(list);
    }

    /**
     * 导出同步任务列表
     */
    @PreAuthorize("@ss.hasPermi('xdata:kettle:export')")
    @Log(title = "同步任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XdataKettle xdataKettle)
    {
        List<XdataKettle> list = xdataKettleService.selectXdataKettleList(xdataKettle);
        ExcelUtil<XdataKettle> util = new ExcelUtil<XdataKettle>(XdataKettle.class);
        util.exportExcel(response, list, "同步任务数据");
    }

    /**
     * 获取同步任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('xdata:kettle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(xdataKettleService.selectXdataKettleById(id));
    }

    /**
     * 新增同步任务
     */
    @PreAuthorize("@ss.hasPermi('xdata:kettle:add')")
    @Log(title = "同步任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody XdataKettle xdataKettle)
    {
        return toAjax(xdataKettleService.insertXdataKettle(xdataKettle));
    }

    /**
     * 修改同步任务
     */
    @PreAuthorize("@ss.hasPermi('xdata:kettle:edit')")
    @Log(title = "同步任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody XdataKettle xdataKettle)
    {
        return toAjax(xdataKettleService.updateXdataKettle(xdataKettle));
    }

    /**
     * 删除同步任务
     */
    @PreAuthorize("@ss.hasPermi('xdata:kettle:remove')")
    @Log(title = "同步任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xdataKettleService.deleteXdataKettleByIds(ids));
    }


    /**
     * 执行一次同步任务
     */
    @PreAuthorize("@ss.hasPermi('xdata:kettle:query')")
    @GetMapping(value = "/run/{id}")
    public AjaxResult run(@PathVariable("id") Long id)
    {
        new Thread(()->{
            SpringUtils.getBean(KettleTask.class).run(id);
        }).start();
        return success("启动成功，请等待同步结果");
    }
}
